package com.taiyi.renaissancemall.ui.mine.wallet.withdraw;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.ReturnMoneyBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.ui.mine.wallet.MyWalletActivity;
import com.taiyi.renaissancemall.ui.mine.withdraw.WithdrawSettingActivity;

import org.greenrobot.eventbus.EventBus;
import org.w3c.dom.Text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity<WithdrawView, WithdrawPresent> implements WithdrawView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.withdrawalAmount)
    TextView withdrawalAmount;
    @BindView(R.id.maximumWithdrawalTv)
    TextView maximumWithdrawalTv;
    @BindView(R.id.withdrawalAmountEt)
    EditText withdrawalAmountEt;
    @BindView(R.id.withdrawalMethod)
    TextView withdrawalMethod;
    @BindView(R.id.rbAliPay)
    RadioButton rbAliPay;
    @BindView(R.id.rbWeChat)
    RadioButton rbWeChat;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.unBoundTv)
    TextView unBoundTv;
    @BindView(R.id.withdrawTv)
    TextView withdrawTv;
    @BindView(R.id.top)
    ConstraintLayout top;
    private String rType = "";//1微信2支付宝
    private String weChatBindId = "";
    private ReturnMoneyBean returnMoneyBean;
    private String tel;
    private String phone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public WithdrawPresent initPresenter() {
        return new WithdrawPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.withdraw));
        phone = getIntent().getStringExtra("tel");
        RegexUtil.pointEdit(withdrawalAmountEt, false);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        presenter.getWithdrawDetail();
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getWithdrawSuccess(ReturnMoneyBean returnMoneyBean) {
        if (returnMoneyBean.user != null) {
            tel = returnMoneyBean.user.tel;
        } else {
            tel = phone;
        }
        this.returnMoneyBean = returnMoneyBean;
        if ("-1".equals(returnMoneyBean.sz)) {
            showProgress();
            countDown("用户未绑定账户，请至少绑定一个账户，跳转绑定");
        } else {
            if (returnMoneyBean.user.liquannumti != null) {
                String maxTv = getString(R.string.maximumWithdrawal) + ":" + returnMoneyBean.user.liquannumti;
                maximumWithdrawalTv.setText(maxTv);
            } else {
                maximumWithdrawalTv.setText("0");
            }
            rType = returnMoneyBean.bindinglist.get(0).bindingid;
            for (int i = 0; i < returnMoneyBean.bindinglist.size(); i++) {
                if (returnMoneyBean.bindinglist.get(i).type.equals("1")) {
                    rbWeChat.setVisibility(View.VISIBLE);
                } else {
                    rbAliPay.setVisibility(View.VISIBLE);
                }
            }

            //1微信2支付宝
            String chooseType = returnMoneyBean.bindinglist.get(0).type;
            if (!"".equals(chooseType)) {
                if ("1".equals(chooseType)) {
                    rbWeChat.setSelected(true);
                } else if ("2".equals(chooseType)) {
                    rbAliPay.setSelected(true);
                }
            }
        }
    }

    @Override
    public void getWithdrawFailure(String msg) {
        ToastUtils.showLongToast(msg);
        if (msg.contains("请设置提现方式")) {
            showProgress();
            countDown("用户未绑定账户，请至少绑定一个账户，跳转绑定");
        } else {
            countDown(msg);
        }
    }

    @Override
    public void toWithdrawSuccess(String msg_sn) {
        ToastUtils.showLongToast("提现成功");
        Intent intent = new Intent(WithdrawActivity.this, MyWalletActivity.class);
        intent.putExtra("toWithdrawSuccess", 1);
        startActivity(intent);
        finish();
    }

    @Override
    public void toWithdrawFailure(String msg) {
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void getNickName(String msg_cn) {

    }

    @OnClick({R.id.backIv, R.id.withdrawTv, R.id.rbAliPay, R.id.rbWeChat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.rbAliPay:
//                rType = "2";
                for (ReturnMoneyBean.BindinglistBean bindinglistBean : returnMoneyBean.bindinglist) {
                    if (bindinglistBean.type.equals("2")) {
                        rType = bindinglistBean.bindingid;
                    }
                }
                rbAliPay.setSelected(true);
                rbWeChat.setSelected(false);
                break;
            case R.id.rbWeChat:
//                rType = "1";
                for (ReturnMoneyBean.BindinglistBean bindinglistBean : returnMoneyBean.bindinglist) {
                    if (bindinglistBean.type.equals("1")) {
                        rType = bindinglistBean.bindingid;
                    }
                }
                rbAliPay.setSelected(false);
                rbWeChat.setSelected(true);
                break;
            case R.id.withdrawTv:
                if (!TextUtils.isEmpty(withdrawalAmountEt.getText().toString().trim())) {
                    if (!TextUtils.isEmpty(rType)) {
                        presenter.toWithdraw(rType, withdrawalAmountEt.getText().toString().trim());
                    } else {
                        ToastUtils.showLongToast("请选择提现方式");
                    }
                } else {
                    ToastUtils.showLongToast("金额不能为空");
                }
                break;
        }
    }

    //倒计时
    private void countDown(String msg) {

        CountDownTimer timer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                String showText = msg + "(0" + millisUntilFinished / 1000 + ")";
                unBoundTv.setVisibility(View.VISIBLE);
                unBoundTv.setText(showText);
            }

            @Override
            public void onFinish() {
                unBoundTv.setText("");
                hideProgress();
                Intent intent = new Intent(WithdrawActivity.this, WithdrawSettingActivity.class);
                if (tel != null && !TextUtils.isEmpty(tel)) {
                    intent.putExtra("tel", tel);
                } else {
                    intent.putExtra("tel", phone);
                }
                startActivity(intent);
                finish();
            }
        }.start();

    }

}
